package com.itheima.controller;

import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckItemService;
import com.itheima.health.utils.resources.MessageConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;


import java.util.List;

@RestController
@Api(tags = "检查项模块管理")
public class CheckItemController {

    @Reference
    private CheckItemService checkItemService;


    //   开发 检查项模块   crud
    @GetMapping("checkitem/findAll")
    @ApiOperation(value = "查询检查项方法", notes = "查询所有的检查项列表信息")
    public Result findAll() {
        try {
            List<CheckItem> list = checkItemService.findAll();
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }

    }

    //添加模块
    @PostMapping("checkitem/add")
    @ApiOperation(value = "新增检查项方法", notes = "新增检查项列表信息")
    //  方法请求参数封装对象  结果响应json采用Result对象封装
    public Result add(@RequestBody CheckItem checkItem) {
        try {
            //  调用mybati-plus提供的方法即可
            boolean save = checkItemService.save(checkItem);
            return new Result(save, MessageConstant.ADD_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
    }


    //   开发 检查项模块   crud
    @PostMapping("checkitem/findPage")
    @ApiOperation(value = "分页查询检查项方法", notes = "分页查询的检查项列表信息")
    @PreAuthorize("hasAnyAuthority('CHECKITEM_QUERY')")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        try {
            PageResult pageResult = checkItemService.findPage(queryPageBean);
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }

    }

    //删除模块
    @DeleteMapping("checkitem/delete")
    @ApiOperation(value = "删除检查项方法", notes = "删除检查项列表信息")
    //  方法请求参数封装对象  结果响应json采用Result对象封装
    public Result delete(@RequestParam("id") Integer id) {
        //判断检查项是否被检查组使用
        boolean bx = checkItemService.findById(id);
        if (bx) {
            //先获取要删除的信息对象id   创建对象  修改数据库的逻辑值为1
            CheckItem checkItem = new CheckItem();
            checkItem.setId(id);
            checkItem.setIs_delete(1);
            boolean b = checkItemService.updateById(checkItem);
            return new Result(b, MessageConstant.DELETE_CHECKITEM_SUCCESS);
        }else {
            return new Result(false, MessageConstant.DELETE_CHECKITEM_FAILs);
        }
    }

    //编辑修改
    @PostMapping("checkitem/update")
    @ApiOperation(value = "编辑修改检查项方法", notes = "编辑修改检查项列表信息")
        //  方法请求参数封装对象  结果响应json采用Result对象封装
         public Result update(@RequestBody CheckItem checkItem) {
            //有一个方法 修改或者添加  系统自动选择  有该数据时修改  无改该数据时添加
        Result result = null;
        try {
            boolean b = checkItemService.saveOrUpdate(checkItem);
            result = new Result(b, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            result = new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
        return result;
    }
}
